﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static LoanOperations.LoanOperations;

namespace Bank_database_system.业务部.贷款业务
{
    public partial class 贷款查询c : UserControl
    {
        public event EventHandler ButtonBack;
        贷款查询d loanSearching = new 贷款查询d();
        private string branch_id = "";
        private string staff_id = "";
        public 贷款查询c()
        {
            InitializeComponent();
            loanSearching.ButtonBack += BackToC;
        }

        public void branch_and_staff_get(string branchID, string staffID)
        {
            branch_id = branchID;
            staff_id = staffID;
        }

        private void BackToC(object sender, EventArgs e)
        {
            // 修改控件状态      
            panel1.Visible = false;
            panel1.Controls.Clear();
            CardID_Set(textBox5.Text);
        }
        private void button8_Click(object sender, EventArgs e)
        {
            textBox5.Text = "";

            ButtonBack?.Invoke(this, EventArgs.Empty);
        }

        public void CardID_Set(string cardID)
        {
            textBox5.Text = cardID;
            //其它
            DataTable dataTable = GetLoansByCardID(cardID);
            label11.Text = "共   " + dataTable.Rows.Count + "   条记录";
            try
            {
                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AutoGenerateColumns = false; // 禁用自动生成列
                dataGridView1.AllowUserToAddRows = false; // 禁用添加空白行
                dataGridView1.Columns.Clear(); // 清除已有列
                dataGridView1.DoubleBuffered(true); //双缓冲

                // 添加 LOAN_ID 列
                DataGridViewTextBoxColumn loanIdColumn = new DataGridViewTextBoxColumn
                {
                    Name = "LOAN_ID",
                    HeaderText = "贷款ID",
                    DataPropertyName = "LOAN_ID",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(loanIdColumn);

                // 添加 AMOUNT 列
                DataGridViewTextBoxColumn amountColumn = new DataGridViewTextBoxColumn
                {
                    Name = "AMOUNT",
                    HeaderText = "贷款数额",
                    DataPropertyName = "AMOUNT",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(amountColumn);

                // 添加 INTEREST_RATE 列
                DataGridViewTextBoxColumn rateColumn = new DataGridViewTextBoxColumn
                {
                    Name = "INTEREST_RATE",
                    HeaderText = "利率",
                    DataPropertyName = "INTEREST_RATE",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(rateColumn);

                // 添加 END_TIME 列
                DataGridViewTextBoxColumn endTimeColumn = new DataGridViewTextBoxColumn
                {
                    Name = "END_TIME",
                    HeaderText = "到期时间",
                    DataPropertyName = "END_TIME",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(endTimeColumn);

                // 添加 STATE 列
                DataGridViewTextBoxColumn stateColumn = new DataGridViewTextBoxColumn
                {
                    Name = "STATE",
                    HeaderText = "状态",
                    DataPropertyName = "STATE",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(stateColumn);

                DataGridViewButtonColumn modifyButtonColumn = new DataGridViewButtonColumn
                {
                    Name = "Modify",
                    HeaderText = "查看信息",
                    Text = "查看",
                    UseColumnTextForButtonValue = true,
                    Width = 100,
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(modifyButtonColumn);
                dataGridView1.CellClick += DataGridView1_CellClick;

                dataGridView1.DataSource = dataTable; // 绑定数据
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 列宽自动调整模式
            }
            catch (Exception ex)
            {
                // 错误处理

            }
        }

        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确保点击的是数据行和按钮列
            if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Modify"].Index)
            {
                // 获取当前行的数据
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                string loanID = row.Cells["LOAN_ID"].Value?.ToString();

                panel1.Visible = true;
                panel1.Controls.Clear();
                loanSearching.Loan_Set(textBox5.Text, loanID);
                loanSearching.branch_and_staff_get(branch_id, staff_id);
                panel1.Controls.Add(loanSearching);
                panel1.BringToFront();//显示置顶
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
